package com.gxuwz.PangGuangyao.business.action.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.PangGuangyao.business.entity.SysRepair;
import com.gxuwz.PangGuangyao.business.service.ISysRepairService;
import com.gxuwz.PangGuangyao.util.ExportExcel;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.util.ExcelUtil;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ExportExcelAction extends BaseAction implements Preparable,ModelDriven{

	private Result<SysRepair> pageResult; //分页
	private SysRepair sysRepair;
	@Autowired
	private ISysRepairService sysRepairService;
	ExportExcel<SysRepair>exportExcel;
	private String  fileName;//导出文件名
	private  ByteArrayOutputStream out;
	private  InputStream  excelStream;
	
	
	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	@Override
	public Object getModel() {
		
		return sysRepair;
	}

	@Override
	public void prepare() throws Exception {
		if(null==sysRepair)
		 sysRepair =new SysRepair();
	}
	
	
	public String getInputStream() throws Exception{
		
		  
		String logtype = getRequest().getParameter("logtype");
		getRequest().getSession().setAttribute("logtype", logtype);
		pageResult=sysRepairService.findAll(getPage(), getRow(),logtype);
		
		if(null==exportExcel)
		{
			exportExcel=new ExportExcel<SysRepair>();
		}
		//将OutputStream转化为InputStream
 
        String[] headers = { "维修编号", "维修费用", "维修原因", "维修状态" };
      			
		//exportExcel.exportExcel(headers, pageResult.getData(), out);
		out = new ByteArrayOutputStream();
		HSSFWorkbook workbook = ExcelUtil.toExcel(headers,pageResult.getData(), out); //这个为调用service层返回的HSSFWorkbook对象
	       ByteArrayOutputStream output = new ByteArrayOutputStream();
	       workbook.write(output);
	       byte[] ba = output.toByteArray();
	       excelStream = new ByteArrayInputStream(ba);
	       output.flush();
	       output.close();
		  return  SUCCESS ;
		
	}

	public Result<SysRepair> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysRepair> pageResult) {
		this.pageResult = pageResult;
	}

	public SysRepair getSysRepair() {
		return sysRepair;
	}

	public void setSysRepair(SysRepair sysRepair) {
		this.sysRepair = sysRepair;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}



}
